
package network.httpsServer;

import java.net.Socket;
import java.util.Date;

import javax.net.ssl.SSLSocket;

import network.utils.ProxyConstants;

/**
 * Local Https Server. User first connects to this SSL server through SSL tunnel
 * proxy.
 * 
 * @author http://arunava.in
 * 
 */
public class HttpsServerThread extends Thread {
	private SSLSocket sslsocket;

	private int whichSSLServer;

	public HttpsServerThread(final SSLSocket sslsocket, final int whichSSLServer) {
		this.sslsocket = sslsocket;
		this.whichSSLServer = whichSSLServer;
	}

	public void run() {
		if (ProxyConstants.logLevel >= 1) {
			System.out.println(new Date() + " HttpsServerThread Started :: "
					+ currentThread());
		}
		try {
			if (ProxyConstants.logLevel >= 1) {
				System.out
						.println(new Date()
								+ " HttpsServerThread Connectiong to local httpProxy server :: "
								+ currentThread());
			}

			final Socket toHttpProxy_Recur = new Socket(
					ProxyConstants.HTTPProxyHost, ProxyConstants.HTTPProxyPort);
			// Read from out Browser and write to server Start
			final HttpsServerBrowserThread thread2 = new HttpsServerBrowserThread(
					toHttpProxy_Recur, sslsocket,whichSSLServer);
			thread2.start();

			// Read from out server and write to browser Start
			final HttpsServerOutgoingThread thread1 = new HttpsServerOutgoingThread(
					toHttpProxy_Recur, sslsocket);
			thread1.start();

			if (ProxyConstants.logLevel >= 1) {
				System.out.println(new Date()
						+ " HttpsServerThread :: Finished :: "
						+ currentThread());
			}

		} catch (final Exception exception) {
			if (ProxyConstants.logLevel >= 1) {
				System.out.println(new Date() + " HttpsServerThread Error :: "
						+ currentThread() + " " + exception);
			}
		}
	}
}
